﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/**
 * MAP通过文本文件生成地图实例
 */
public class Map : MonoBehaviour
{
    private const int MAP_ORIGIN_X = 0, MAP_ORIGIN_Z = 0;
    
    public TextAsset[] m_map_textasset;
    private static TextAsset curMap;
    public GameObject block,gem,knight,sword,treasure,ghost;
    public GameObject Ground;
    public GameObject[] item_group = new GameObject[4];
    public int NumOfGem = 0, NumOfEne = 0;
    public const char GEM = 'c';

    public const char BLOCK = '*';

    public const char KNIGHT = 'p';
    
    public const char SWORD = 's';
    
    public const char TREASURE = 't';

    public const float yUp = 0.36f; 
    public enum GHOST
    {
        TYPE1 = 1,
        TYPE2 = 2,
        TYPE3 = 3,
        TYPE4 = 4
    }

    private void Start()
    {
        item_group[0] = GameObject.Find("Enemy");
        item_group[1] = GameObject.Find("Player");
        item_group[2] = GameObject.Find("Prop");
        item_group[3] = GameObject.Find("Block");
    }

    struct MapData//地图数据
    {
        public int length, width;//地图长宽
        public Vector2 offsetpos;//data[0][0]位置
        public char[,] data;//data[i][j]存储每个数据
        public float[,] height;
        public int[,] gemPartical;
    }
    [SerializeField] private MapData map_data;
    // Start is called before the first frame update
    

    // Update is called once per frame
    void Update()
    {
        
    }

    public void SetMap(int order)
    {
        curMap = m_map_textasset[order];
    }

    public void CreateMap()
    {
        LoadPromAsset(curMap);
        InstantMap();
    }
    private void LoadPromAsset(TextAsset assset)
    {
        map_data.offsetpos = new Vector2(MAP_ORIGIN_X,MAP_ORIGIN_Z);
        String content = assset.text;
        System.StringSplitOptions options = System.StringSplitOptions.RemoveEmptyEntries;//删除空元素
        String[] lines = content.Split(new char[] {'\r', '\n'}, options);
        char[] spliter = new char[] {','};
        String[] size = lines[0].Split(spliter, options);//第一行是尺寸
        map_data.width = int.Parse(size[0]);
        map_data.length = int.Parse(size[1]);
        char[,] data = new char[map_data.length,map_data.width];
        for (int i = 0; i < map_data.length; i++)
        {
            String[] temp = lines[map_data.length - i].Split(spliter, options);
            for (int j = 0; j < map_data.width; j++)
            {
                data[i, j] = temp[j][0];
            }
        }

        map_data.data = data;
    }
    

    private void InstantMap()
    {
        NumOfEne = NumOfGem = 0;
        for (int x = 0; x < map_data.width; x++)
        {
            for (int z = 0; z < map_data.length; z++)
            {
                Vector3 bpos = new Vector3(x+map_data.offsetpos.x,0,z+map_data.offsetpos.y);
                switch (map_data.data[z,x])
                {
                    case BLOCK:
                    {
                        GameObject o = Instantiate(block,bpos,Quaternion.identity);
                        o.transform.parent = Ground.transform;
                            o.AddComponent<BoxCollider>();
                            o.transform.parent = item_group[3].transform;
                    }
                        
                        break;
                    case GEM:
                    { 
                        GameObject o = Instantiate(gem,bpos,Quaternion.identity);
                        o.transform.parent = Ground.transform;
                        o.transform.parent = item_group[2].transform;
                        NumOfGem++;
                    }
                        break;
                    case KNIGHT:
                    {
                        GameObject o = Instantiate(knight,bpos+new Vector3(0,yUp,0),Quaternion.identity);
                        o.transform.parent = Ground.transform;
                            o.AddComponent<CharaAnimator>();
                            o.AddComponent<PlayerControl>();
                            o.AddComponent<GridMove>();
                            o.transform.parent = item_group[1].transform;
                    }
                        break;
                    case SWORD:
                    {
                        GameObject o = Instantiate(sword,bpos,Quaternion.identity);
                        o.transform.parent = Ground.transform;
                        o.AddComponent<ItemSwordScr>();
                        o.GetComponent<Renderer>().enabled = true;
                        o.transform.parent = item_group[2].transform;
                    }
                        break;
                    case TREASURE:
                    {
                        GameObject o = Instantiate(treasure,bpos,Quaternion.identity);
                        o.transform.parent = Ground.transform;
                        o.transform.parent = item_group[2].transform;
                    }
                        break;
                    case '1':
                    {
                        GameObject o = Instantiate(ghost,bpos+new Vector3(0,yUp,0),Quaternion.identity);
                        o.transform.parent = Ground.transform;
                            o.AddComponent<MonsterCtrl>();
                            o.AddComponent<GridMove>();
                            o.AddComponent<CharaAnimationMonster>();
                            o.GetComponent<MonsterCtrl>().SetType(1);
                            o.transform.parent = item_group[0].transform;
                            NumOfEne++;
                    }
                        break;
                    case '2':
                    {
                        GameObject o = Instantiate(ghost,bpos+new Vector3(0,yUp,0),Quaternion.identity);
                        o.transform.parent = Ground.transform;
                            o.AddComponent<MonsterCtrl>();
                            o.AddComponent<GridMove>();
                            o.AddComponent<CharaAnimationMonster>();
                            o.GetComponent<MonsterCtrl>().SetType(2);
                            o.transform.parent = item_group[0].transform;
                            NumOfEne++;
                        }
                        break;
                    case '3' :
                    {
                        GameObject o = Instantiate(ghost,bpos+new Vector3(0,yUp,0),Quaternion.identity);
                        o.transform.parent = Ground.transform;
                            o.AddComponent<MonsterCtrl>();
                            o.AddComponent<GridMove>();
                            o.AddComponent<CharaAnimationMonster>();
                            o.GetComponent<MonsterCtrl>().SetType(3);
                            o.transform.parent = item_group[0].transform;
                            NumOfEne++;
                        }
                        break;
                    case '4':
                    {
                        GameObject o = Instantiate(ghost,bpos+new Vector3(0,yUp,0),Quaternion.identity);
                        o.transform.parent = Ground.transform;
                            o.AddComponent<MonsterCtrl>();
                            o.AddComponent<GridMove>();
                            o.AddComponent<CharaAnimationMonster>();
                            o.GetComponent<MonsterCtrl>().SetType(4);
                            o.transform.parent = item_group[0].transform;
                            NumOfEne++;
                        }
                        break;
                }
            }
        }
    }
}
